﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CrmApplication.Models;

namespace CrmApplication.Controllers
{
    public class DivisionController : Controller
    {
        //
        // GET: /Division/

        public ActionResult Index()
        {
            return View();
        }


        /// <summary>
        /// get all divisions 
        /// </summary>
        /// <returns></returns>
        public ActionResult GetDivision()
        {
            string searchDiv = Request.Params["query"];

            DivisionContainer container = DivisionModels.GetDivArray(searchDiv);
            return Json(container, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// add new division
        /// </summary>
        /// <returns></returns>
        public ActionResult AddDivision()
        {
            string div = Request.Params["data"];
            string messageBack = null;

            DivisionModels chechDiv = new DivisionModels();
            chechDiv = DivisionModels.CheckDiv(div);

            try
            {
                if (div != "")
                {
                    if (chechDiv == null)
                    {
                        DivisionModels.AddDivision(div);
                    }
                    else
                    {
                        messageBack = "Departamentul exista deja!";
                    }
                }

                return Json(new { success = true, description = messageBack });
            }
            catch (Exception ex)
            {
				if (ex.Message.Contains("unique key constraint"))
				{
					messageBack = "Exista deja o inregistrare cu acelasi nume";
				}
				return Json(new { success = true, description = messageBack });
            }
        }
    }
}
